package com.gx.smart.building.ui.repair.fragment

import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentRepairBinding
import com.gx.smart.building.ui.repair.adapter.PageAdapter
import com.gx.smart.building.ui.repair.viewmodel.RepairViewModel
import com.gx.smart.lib.event.LoadingMessageEvent
import org.greenrobot.eventbus.EventBus

class RepairFragment : BaseMVVMFragmentV2<RepairViewModel>() {
    private lateinit var viewBinding: FragmentRepairBinding
    private lateinit var mPagerAdapter: PageAdapter
    override fun onBindLayout(): Int = R.layout.fragment_repair

    override fun getToolbarTitle(): String = "我的工单"

    override fun onBindViewModel(): Class<RepairViewModel> = RepairViewModel::class.java
    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentRepairBinding.inflate(layoutInflater, root, true)
    }

    override fun initContent() {
        super.initContent()
        mPagerAdapter = PageAdapter(requireActivity())
        viewBinding.viewPager.adapter = mPagerAdapter
        viewBinding.viewPager.isUserInputEnabled = true
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().post(LoadingMessageEvent(true))

        viewModel.countStatusQuantity().observe(viewLifecycleOwner) {
            EventBus.getDefault().post(LoadingMessageEvent(false))
            val titles = resources.getStringArray(R.array.repair)
            TabLayoutMediator(
                viewBinding.tabLayout, viewBinding.viewPager
            ) { tab: TabLayout.Tab, position: Int ->
                tab.text = when (position) {
                    0 -> titles[position] + "(${it.task_receive})"
                    1 -> titles[position] + "(${it.task_process})"
                    2 -> titles[position] + "(${it.task_suspend_review + it.task_suspend})"
                    3 -> titles[position] + "(${it.task_review + it.task_complete})"
                    else -> "错误"
                }
            }.attach()
        }
    }
}